﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TCHSHue.BLL;

namespace TCHSHue.Modules
{
    public partial class Index : System.Web.UI.Page
    {
        #region Commented
        //protected override void OnInit(EventArgs e)
        //{
        //    base.OnInit(e);
        //    if (Request.QueryString["mode"] == "hoidap")
        //    {
        //        //   Page.EnableViewState = false;
        //    }
        //} 
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            #region Commented
            /*string pathFile = Server.MapPath("~/DAL/count.txt");
            userOnline.Text = Application["OnlineUser"].ToString();
            Ultil ultil = new Ultil(pathFile);
            totalOnline.Text = ultil.GetTotal().ToString();
            var user = (sinhvien)Session["userDangNhap"];
            if (user != null)
            {
                lnkUser.Text = user.ho + " " + user.ten;
                lnkUser.PostBackUrl = "~/Modules/Index.aspx?mode=4&id=" + user.masinhvien;
                dangNhapDiv.Visible = false;
                daDaDangNhap.Visible = true;
                if (user.vaitro != null && user.vaitro == (int)VAITRO.Admin)
                    quanTriDiv.Visible = true;
                else
                    quanTriDiv.Visible = false;
            }*/

            #endregion

            lbNgayThang.Text = string.Format("Thứ {0}, ngày {1} tháng {2} năm {3}", GetDayOfWeek(DateTime.Now.DayOfWeek), DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
            userOnline.Text = Application["OnlineUser"].ToString();
            DisplayLKWS();
            divTimKiem.Controls.Add(LoadControl("~/Modules/IndexControl/TimKiem/ctrWigetTK.ascx"));
            //slideShow.Controls.Add(LoadControl("~/Modules/IndexControl/Slider/ctrImageSlider.ascx"));

            #region Select UserControl
            var mode = Request.QueryString["mode"];
            Control ctr = null;
            if (!string.IsNullOrEmpty(mode))
            {
                //imageSlide.Visible = false;
                //galleria.Visible = false;
                switch (mode)
                {
                    case "err":
                        ctr = LoadControl("~/Modules/IndexControl/ctrLoi.ascx");
                        break;
                    case "tintuc_sukien":
                        ctr = LoadControl("~/Modules/IndexControl/TinTucSuKien.ascx");
                        break;
                    case "hoidap":
                        ctr = LoadControl("~/Modules/IndexControl/HoiDap/ctrHoiDap.ascx");
                        break;
                    case "chitiettintuc":
                        ctr = LoadControl("~/Modules/IndexControl/TinTuc/ctrTinTucChiTiet.ascx");
                        break;
                    case "danhsachtintuc":
                        ctr = LoadControl("~/Modules/IndexControl/TinTuc/ctrTinTucList.ascx");
                        break;
                    case "gioithieu":
                        ctr = LoadControl("~/Modules/IndexControl/GioiThieu/ctrGioiThieu.ascx");
                        break;
                    case "bieumau":
                        ctr = LoadControl("~/Modules/IndexControl/BieuMau/ctrBieuMau.ascx");
                        break;
                    case "thutuchanhchinh":
                    case "luat":
                    case "nghidinh":
                    case "thongtu":
                    case "thongtulientich":
                    case "quyetdinh":
                    case "quyhoach":
                        ctr = LoadControl("~/Modules/IndexControl/ThuTucHanhChinh/ctrThuTucHanhChinh.ascx");
                        break;
                    case "tracuuhoso":
                        ctr = LoadControl("~/Modules/IndexControl/TraCuu/ctrTraCuuHoSo.ascx");
                        break;
                    case "timkiem":
                        ctr = LoadControl("~/Modules/IndexControl/TimKiem/ctrTimKiem.ascx");
                        break;
                    case "bando":
                        ctr = LoadControl("~/Modules/IndexControl/Bando/ctrBando.ascx");
                        break;
                    default:
                        Response.Redirect("~/Modules/Index.aspx");
                        break;
                }
            }
            else
            {
                Control ctrTinTuc = LoadControl("~/Modules/IndexControl/TinTuc/ctrTinTucIndex.ascx");
                Control ctrTraCuuHoSo = LoadControl("~/Modules/IndexControl/TraCuu/ctrTraCuuHoSo.ascx");
                divTinTuc.Controls.Add(ctrTinTuc);
                divTraCuu.Controls.Add(ctrTraCuuHoSo);
               
            }
            if (ctr != null)
                content.Controls.Add(ctr);
            createMenu(mode);
            //CreateSlideImage();
            //imageSlide.Controls.Add(LoadControl("~/Modules/IndexControl/Slider/ctrImageSlider.ascx"));
            #endregion
        }
        private string GetDayOfWeek(DayOfWeek dayOfWeek)
        {
            switch (dayOfWeek)
            {
                case DayOfWeek.Monday: return "hai";
                case DayOfWeek.Tuesday: return "ba";
                case DayOfWeek.Wednesday: return "bốn";
                case DayOfWeek.Thursday: return "năm";
                case DayOfWeek.Friday: return "sáu";
                case DayOfWeek.Saturday: return "bảy";
                case DayOfWeek.Sunday: return "chủ nhật";
            }
            return "không rõ";
        }
        private void DisplayLKWS()
        {
            var lsLK = LKWSBLL.getList();
            var htmlLK = "<ul class='index_menuleft'>";
            foreach (var lk in lsLK)
            {
                htmlLK += string.Format("<li><a href='{0}' title='{1}' target='blank'>{1}</a></li>", lk.url, lk.tenHienThi);
            }
            htmlLK += "</ul>";
            lkWebsite.InnerHtml = htmlLK;
        }
        void createMenu(string mode)
        {
            bool[] menu = new bool[7];
            switch (mode)
            {
                case "":
                case null:
                    menu[0] = true;
                    break;
                case "tintuc_sukien":
                    break;
                    //case "hoidap":
                    //    menu[5] = true;
                    break;
                case "bieumau":
                    menu[3] = true;
                    break;
                case "gioithieu":
                    var id = Request.QueryString["id"];
                    var idBV = Request.QueryString["idBaiViet"];
                    if (string.IsNullOrEmpty(idBV))
                    {
                        if (id == "thutuchanhchinh")
                            menu[2] = true;
                        else
                            menu[1] = true;
                    }
                    else
                    {
                        var bv = BaiVietBLL.getBaiViet(idBV);
                        if (bv.maTheLoai.Value == Hang.LUAT || bv.maTheLoai.Value == Hang.NGHIDINH || bv.maTheLoai.Value == Hang.THONGTU || bv.maTheLoai.Value == Hang.THONGTULIENTICH || bv.maTheLoai.Value == Hang.QUYETDINH)
                            menu[4] = true;
                        else if (bv.maTheLoai.Value == Hang.THUTUCHANHCHINH)
                            menu[2] = true;

                    }
                    break;
                case "thutuchanhchinh":
                    menu[2] = true;
                    break;
                case "luat":
                case "nghidinh":
                case "thongtu":
                case "thongtulientich":
                case "quyetdinh":
                case "quyhoach":
                case "bando":
                    menu[4] = true;
                    break;
                case "danhsachtintuc":
                    var idTL = Request.QueryString["idTL"];
                    if (idTL == Hang.THONGBAO.ToString())
                        menu[6] = true;
                    break;
                default:
                    menu[0] = true;
                    break;
            }
            string style = @"class='selected boderRight'style='color:white;text-shadow: 0.0em 0.08em 0.05em #333;'";
            string nonstyle = "class='unselected boderRight'";
            string strMenu = string.Format(@"<ul>
                            <li><a {0} href='Index.aspx' title=''><span class='tieuDe Home'></span><span>Trang chủ</span></a> </li>
                            <li><a {1} href='#' title=''><span class='tieuDe GT'></span><span>Giới thiệu</span></a>
                                <ul>
                                    <li><a href='Index.aspx?mode=gioithieu&id=cocautochuc' title=''>Cơ cấu tổ chức</a></li>
                                    <li><a href='Index.aspx?mode=gioithieu&id=nhiemvu' title=''>Nhiệm vụ</a></li>
                                </ul>
                            </li>
                            <li><a {2} href='Index.aspx?mode=thutuchanhchinh' title=''><span class='tieuDe TTHC'></span><span>Thủ tục hành chính</span></a></li>
                            <li><a {3} href='Index.aspx?mode=bieumau' title=''><span class='tieuDe BM'></span><span>Biểu mẫu</span></a></li>
                            <li><a {4} href='#' title=''><span class='tieuDe LW'></span><span>Văn bản pháp luật</span></a>
                                <ul>
                                    <li><a href='Index.aspx?mode=luat' title=''>Luật</a></li>
                                    <li><a href='Index.aspx?mode=nghidinh' title=''>Nghị định</a></li>
                                    <li><a href='Index.aspx?mode=thongtu' title=''>Thông tư</a></li>
                                    <li><a href='Index.aspx?mode=thongtulientich' title=''>Thông tư liên tịch</a></li>
                                    <li><a href='Index.aspx?mode=quyetdinh' title=''>Quyết định</a></li>
                                    <li><a href='Index.aspx?mode=quyhoach' title=''>Thông tin quy hoạch</a></li>
                                    <li><a href='Index.aspx?mode=bando' title=''>Bản đồ</a></li>
                                </ul>
                            </li>
                            {5}
                            <li><a {6} href='Index.aspx?mode=danhsachtintuc&idTL=10' title=''><span class='tieuDe TB'></span><span>Thông báo</span></a></li>
                            <li><a href='Admin.aspx' title=''><span class='tieuDe QT'></span><span>Quản trị</span></a></li>
                        </ul>", menu[0] ? style : nonstyle, menu[1] ? style : nonstyle, menu[2] ? style : nonstyle, menu[3] ? style : nonstyle, menu[4] ? style : nonstyle, /*menu[5] ? style : nonstyle*/"", menu[6] ? style : nonstyle);
            submainmenu.InnerHtml = strMenu;
            //<li><a {5} href='Index.aspx?mode=hoidap' title=''>HỎI ĐÁP</a></li>
        }
        private void CreateSlideImage()
        {
            //Lấy danh sách các file ảnh để làm slide image
            DirectoryInfo diFiles = new DirectoryInfo(Server.MapPath("~/Upload/SlideImage"));
            var listFile = diFiles.GetFiles("*.*");
            if (listFile == null || listFile.Count() == 0)
            {
                //imageSlide.Visible = false;
                return;
            }
            var createHTMLSlideImage = "";
            foreach (var fileImage in listFile)
            {
                if (!string.IsNullOrEmpty(createHTMLSlideImage))
                    createHTMLSlideImage += "|";
                if (fileImage.Extension == ".png" || fileImage.Extension == ".jpg" || fileImage.Extension == ".gif")
                    createHTMLSlideImage += fileImage.Name;
                //                    createHTMLSlideImage += string.Format(@"<a href='../../Upload/SlideImage/{0}' title='a'>
                //                                                            <img src='../../Upload/SlideImage/{0}' alt='' /></a>", fileImage.Name);
            }
            createHTMLSlideImage += "";
            //galleria.InnerHtml = createHTMLSlideImage;
        }
    }
}